const encryptionStr = function(str:string):string{
  return window.encodeURI(str)
},
decryptStr = function(str:string):string{
  return window.decodeURI(str)
};
const localStorage = window.localStorage;

const localSave = (name:string,value:any,time?:number)=>{
  var data = localStorage.getItem('APP_ALL_DATA');
  var nowTime = Date.now();
  var newData = data?JSON.parse(decryptStr(data)):{};
  newData[name] = {data:value,time:time?(nowTime+1000*time):0};
  newData = JSON.stringify(newData);
  localStorage.setItem('APP_ALL_DATA',encryptionStr(newData))
}
const localEcho = (name:string)=>{
  var dataStr = localStorage.getItem('APP_ALL_DATA');
  var nowTime = Date.now();
  if(dataStr){
      var newData = JSON.parse(decryptStr(dataStr));
      if(!newData[name]){
          return null;
      }
      var {data,time} = newData[name];
      if(time && nowTime - time > 0){
          return null;
      }
      return data;
  }else{
      return null;
  }  
}
export function $local (name:string,value?:any,time?:number){
  if(value !== undefined){
      localSave(name,value,time)
  }else{
      return localEcho(name)
  }
}